document.addEventListener('DOMContentLoaded', function() {
    // 元素引用
    const timeDisplay = document.getElementById('time');
    const startBtn = document.getElementById('start-btn');
    const pauseBtn = document.getElementById('pause-btn');
    const resetBtn = document.getElementById('reset-btn');
    const progressBar = document.getElementById('progress-bar');
    const modeButtons = document.querySelectorAll('.mode-btn');
    
    // 计时器变量
    let timer;
    let timeLeft = 25 * 60; // 默认25分钟
    let isRunning = false;
    let currentMode = 'pomodoro';
    
    // 模式设置
    const modes = {
        pomodoro: { time: 25 * 60, color: '#ff3b30' },
        'short-break': { time: 5 * 60, color: '#34c759' },
        'long-break': { time: 15 * 60, color: '#0071e3' }
    };
    
    // 初始化显示
    updateDisplay();
    
    // 事件监听器
    startBtn.addEventListener('click', startTimer);
    pauseBtn.addEventListener('click', pauseTimer);
    resetBtn.addEventListener('click', resetTimer);
    
    modeButtons.forEach(button => {
        button.addEventListener('click', function() {
            switchMode(this.dataset.mode);
        });
    });
    
    // 计时器函数
    function startTimer() {
        if (!isRunning) {
            isRunning = true;
            timer = setInterval(updateTimer, 1000);
            startBtn.textContent = '继续';
        }
    }
    
    function pauseTimer() {
        clearInterval(timer);
        isRunning = false;
    }
    
    function resetTimer() {
        pauseTimer();
        timeLeft = modes[currentMode].time;
        updateDisplay();
        startBtn.textContent = '开始';
    }
    
    function updateTimer() {
        timeLeft--;
        updateDisplay();
        
        if (timeLeft <= 0) {
            clearInterval(timer);
            isRunning = false;
            
            // 播放完成音效
            playCompletionSound();
            
            // 自动切换模式
            if (currentMode === 'pomodoro') {
                switchMode('short-break');
            } else {
                switchMode('pomodoro');
            }
            
            startTimer(); // 自动开始下一个阶段
        }
    }
    
    function updateDisplay() {
        const minutes = Math.floor(timeLeft / 60);
        const seconds = timeLeft % 60;
        timeDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        
        // 更新进度条
        const totalTime = modes[currentMode].time;
        const progress = (totalTime - timeLeft) / totalTime;
        progressBar.style.transform = `scaleX(${progress})`;
        progressBar.style.backgroundColor = modes[currentMode].color;
    }
    
    function switchMode(mode) {
        currentMode = mode;
        timeLeft = modes[mode].time;
        
        // 更新活动按钮样式
        modeButtons.forEach(button => {
            if (button.dataset.mode === mode) {
                button.classList.add('active');
            } else {
                button.classList.remove('active');
            }
        });
        
        // 更新显示
        updateDisplay();
        
        // 如果计时器正在运行，继续运行
        if (isRunning) {
            pauseTimer();
            startTimer();
        } else {
            startBtn.textContent = '开始';
        }
    }
    
    function playCompletionSound() {
        const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3');
        audio.play();
    }
});